<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../css/bootstrap.min.css">
    <link rel="stylesheet" href="../layui/css/layui.css"  media="all">
    <script type="application/javascript" src="../js/jquery-3.3.1.js"></script>
    <script type="application/javascript" src="../js/bootstrap.min.js"></script>
    <script type="application/javascript" src="../layui/layui.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>

    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
<body>

<table class="layui-table" lay-data="{url:'/car/findAll', id:'test3'}" lay-filter="test3">

    <thead>
    <tr>
        <th lay-data="{type:'checkbox'}">ID</th>
        <th lay-data="{field:'id', width:80, sort: true}">车号</th>
        <th lay-data="{field:'name', width:120, sort: true, edit: 'text'}">车名</th>
        <th lay-data="{field:'type', width:120, sort: true, edit: 'text'}">车型</th>
        <th lay-data="{field:'sitnum', width:120, sort: true, edit: 'text'}">座位数</th>
        <th lay-data="{field:'cid', width:120, sort: true, edit: 'text'}">门店id</th>
       <th lay-data="{field:'g_pic', title:'商品图片',width:120, sort: true,align:'center',templet:'#imgtmp' ,edit:'text'} ">图片</th>
        <th lay-data="{field:'price', width:120, sort: true, edit: 'text'}">租金</th>
        <th lay-data="{field:'number', width:120, sort: true, edit: 'text'}">预定数</th>
        <th lay-data="{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}">操作</th>
    </tr>
    </thead>
</table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="//res.layui.com/layui/dist/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

<script>
    layui.use('table', function(){
        var table = layui.table;

        table.render({
            elem: '#test'
            ,url:'/test/table/demo1.json'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '用户数据表'
            ,page: true
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test3)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        url:"/car/deleteCar",
                        data:JSON.stringify(data.id),
                        type:"post",
                        contentType:"application/json",
                        success:function (res) {
                            obj.del();
                            layer.close(index);
                        }
                    })

                });
            }
        });
    });
</script>
<script>
    layui.use('table', function(){
        var table = layui.table;

        //监听单元格编辑
        table.on('edit(test3)', function(obj){
            var data = obj.data; //得到所在行所有键值
            $.ajax({
                url:"/car/updateCar",
                type:"post",
                data:JSON.stringify(data),
                contentType:"application/json",
                susccess:function (res) {
                    window.location.reload();
                }
            });

        });
    });
</script>

<script type="textml" id="imgtmp">
    <img src={{d.picture}} style="width: 108px;height: 100px">
    <input type="file" value="选择图片"/>
</script>
</body>
</html>